package com.server;

import java.io.IOException;
import java.sql.SQLException;
import java.util.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.Helper.FlightHelper;
import com.Helper.FlightInfo;
import com.Helper.ReservationHelper;
import com.Helper.ReservationInfo;
import com.Helper.SeatHelper;
import com.Helper.SeatInfo;
import com.database.CustomerDB;

public class StatisticsServlet extends CustomerServlet
{

	private static final long serialVersionUID = 1L;
	private final String className = "Statistics Servlet: ";
	
	@Override
	//@requires HttpServletRequest !=NULL,  HttpServletResponse !=NULL ;    //@assignable flightList,resList,SeatList,flightIDList,username;
	//@signals_only RuntimeException, IOException,ServletException;ArrayStoreException
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException
	{
		ConnectDB();
		ArrayList<FlightInfo> flightList = new ArrayList<FlightInfo>();
		ArrayList<ReservationInfo> resList;
		ArrayList<SeatInfo> seatList= new ArrayList<SeatInfo>();
		ArrayList<String> flightIDList = new ArrayList<String>();
		String username = req.getParameter("username");
		Boolean isEmpty;
		HashMap<String, Integer> classHash = new HashMap<String, Integer>();
		classHash.put("Economy Plus",0);
		classHash.put("Economy",0);
		classHash.put("Business",0);
		try 
		{
			ReservationHelper.AdminSetup(connection, username);

			resList = ReservationHelper.GetAllResList();
			
			
			//seatList = SeatHelper.GetSelectedSeat(seat, flight);
						
			FlightHelper.Setup(connection);
			SeatHelper.Setup(connection);		
			if (resList.size() > 0)
			{
				for (int i = 0; i < resList.size(); i++)
				{
					String flightID = resList.get(i).flightID;
					String seat = resList.get(i).seat;
					String seatClass = SeatHelper.GetSelectedSeat(seat, flightID).type;
					if(!classHash.containsKey(seatClass)){
						classHash.put(seatClass, 1);
					}
				else{
						Integer temp = classHash.get(seatClass);
						temp +=1;
						classHash.put(seatClass,temp);
					}
					
					FlightInfo flight= FlightHelper.GetSelectedFlights(flightID);
					if(!flightIDList.contains(flightID)){
						flightList.add(FlightHelper.GetSelectedFlights(flightID));
						flightIDList.add(flightID);
							
					}
					//flightList.add(FlightHelper.GetSelectedFlights(flightID));
					seatList.add(SeatHelper.GetSelectedSeat(seat, flightID));
				}
			}
			if (flightList.size()==0){
				isEmpty=true;		
			}else{
				isEmpty=false;
			}
			int totalReservationCount = resList.size();

			
			//filling the customers,username,meal hashtable
			HashMap<String, Integer> customerHash = new HashMap<String, Integer>();
			HashMap<String, String> usernameHash = new HashMap<String, String>();
			HashMap<String, Integer> mealHash = new HashMap<String, Integer>();
			HashMap<String, Integer> flightIDHash = new HashMap<String, Integer>();
			mealHash.put("Chicken",0);
			mealHash.put("Vegetarian",0);
			mealHash.put("Beef",0);
			for (int i=0; i<resList.size();i++){
				String key1 = resList.get(i).username;
				String key2 = resList.get(i).meal;
				String key3 = resList.get(i).flightID;
				
				if(customerHash.containsKey(key1)){
					Integer temp = (Integer) customerHash.get(key1);
					customerHash.put(key1, temp+1);
				}else{
					customerHash.put(key1, 1);
				}
				if(flightIDHash.containsKey(key3)){
					Integer temp = (Integer) flightIDHash.get(key3);
					flightIDHash.put(key3, temp+1);
				}else{
					flightIDHash.put(key3, 1);
				}
				if(!usernameHash.containsKey(key1)){
					String temp = resList.get(i).firstName +" "+resList.get(i).lastName;
					usernameHash.put(key1, temp);
				}
				if(mealHash.containsKey(key2)){
					Integer temp = (Integer) mealHash.get(key2);
					mealHash.put(key2, temp+1);
				}else{
					mealHash.put(key2, 1);
				}
			}
			
			ArrayList mealValueArrayList = new ArrayList();
			ArrayList mealStringArrayList = new ArrayList();
			Object[] mealValueArray = mealHash.values().toArray();
			String[] mealStringArray = {"'Chicken'","'Vegetarian'","'Beef'"};
			
			Collections.addAll(mealValueArrayList, mealValueArray); 
			Collections.addAll(mealStringArrayList, mealStringArray); 

			req.setAttribute("resList", resList);
			req.setAttribute("flightList", flightList);
			req.setAttribute("seatList", seatList);
			req.setAttribute("totalReservationCount",totalReservationCount);
			req.setAttribute("mealHash", mealHash);
			req.setAttribute("customerHash", customerHash);
			req.setAttribute("usernameHash", usernameHash);
			req.setAttribute("flightIDHash", flightIDHash);
			req.setAttribute("classHash", classHash);
			req.setAttribute("isEmpty", isEmpty);
			req.setAttribute("mealValueArrayList",mealValueArrayList);
			req.setAttribute("mealStringArrayList",mealStringArrayList);
		} 
		catch (SQLException e) 
		{

			e.printStackTrace();
		}
				
		req.getRequestDispatcher("admin/stats.jsp").forward(req, resp);
	}
}
